<?php

class Client_SettingsController extends App_Controller_Action
{	
	public function indexAction()
	{
		//$this->_redirect("/client/settings/reset-password");
    }
	
	public function resetPasswordAction()
	{
    	$request=$this->getRequest();
    	if($request->isPost())
    	{
    		$email = $request->getParam('email');    	    		
    		$password = $request->getParam('password');    	 
    		$model_client = new Model_UserClient();
			$result = $model_client->udpatePassword($email,$password);
			
			if ($result){
				//for action log
				$data=array();
				$data['ip'] = Model_ActionLog::getIp();			
				$data['user_id'] = 0;
				$data['client_id'] = $this->getUserId();
				$data['dpt_id'] = '';
				$data['dpt_account_id'] = $email;
				$data['action'] = 'update password';
				$data['memo'] = $_SERVER['REQUEST_URI'];	
				Model_ActionLog::actionLog($data);			
						
				$this->_redirect("/client/settings/reset-confirm");	
			}else{
				$this->view->error_msg="Sorry, password reset failed.";					
			}
				
    	}
    }

	public function resetConfirmAction()
	{
    }
    

	public function linkAccountAction()
	{
    	$request=$this->getRequest();
    	$model_service = new Model_ServiceCentral();
    	$user_id = $this->getUserId();
    	
    	if($request->isPost())
    	{
    		$email = $request->getParam('email');    	    		
    		$password = $request->getParam('password');    	    		
    		$result = $model_service->addLinkedAccount($user_id,$email,$password);
			
			if ($result){
				$this->view->success_msg="Account has been linked successfully.";	
				$this->_redirect("/client/settings/link-account");					
			}else{
				$this->view->error_msg="Sorry, account is invalid or duplicated.";					
			}
				
    	}else{
      		$act = $this->getRequest()->getParam('act');
			if ($act=='remove'){
		      	$model_service = new Model_ServiceCentral();
		    	$link_accounts = $model_service->linkedAccounts($user_id);
				
		      	$client_id = $this->getRequest()->getParam('id');
				if(array_key_exists($client_id,$link_accounts[ $user_id ]))
				{				
		    		$result = $model_service->removeLinkedAccount($user_id,$client_id);				
					if ($result){
						$this->view->success_msg="Linked account has been removed successfully.";	
					}else{
						$this->view->error_msg="Sorry, remove failed.";					
					}
				}else{
					$this->view->error_msg="Sorry, remove failed.";										
				}			
				$this->_redirect("/client/settings/link-account");					
			}
    		
    	}
    	
    	$link_accounts = $model_service->linkedAccounts($user_id);
    	//Zend_Debug::dump($link_accounts);exit;
    	$this->view->link_accounts = $link_accounts;
    	$this->view->client_id=$user_id;
    	
    }
    
    public function googleAction()
    { 
    	$return_url = "/client/settings/google-openid";
    	$association_handle = App_GoogleOpenID::getAssociationHandle();
    	
    	
    	$googleLogin = App_GoogleOpenID::createRequest($return_url, $association_handle, true);
    	$googleLogin->redirect();
    	
    	exit("");
    }
    

	public function googleOpenidAction()
	{
    	$request=$this->getRequest();
    	$model_service = new Model_ServiceCentral();
    	$user_id = $this->getUserId();
		
    	$google_id = "";
		$google_email = "";
		
		$googleLogin = App_GoogleOpenID::getResponse();
    	if($googleLogin->success()){
		    $google_id = $googleLogin->identity();
		    $google_email = $googleLogin->email();		    		    
		}
    	//Zend_Debug::dump($google_id);
    	//Zend_Debug::dump($google_email);exit;
		if ($google_id && $google_email)
    	{
    		$result = $model_service->addGoogleLinkedAccount($user_id,$google_email);
			
			if ($result){
				$this->view->success_msg="Account has been linked successfully.";	
			}else{
				$this->view->error_msg="Sorry, account is invalid or duplicated.";					
			}
			$this->_redirect("/client/settings/link-account");					
			
    	}else{
			$this->_redirect("/client/settings/link-account");					    		
    	}
    	exit;
	}
    
}

